package com.yuntu.controller;

import com.alibaba.fastjson.JSONArray;
import com.mysql.jdbc.StringUtils;
import com.yuntu.tools.PageUtil;
import com.yuntu.pojo.Peitems;
import com.yuntu.service.PeitemsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/peitems")
public class PaitemsController {
    private Logger logger= Logger.getLogger(PaitemsController.class);
    @Autowired
    private PeitemsService peitemsService;
    //查询全部
    @RequestMapping(value = "/list")
    public String PeitemsList(HttpServletRequest request){
        try {
            List<Peitems> peitemsList= peitemsService.peitemsAll();
            request.setAttribute("listServie",peitemsList);
            System.out.println(peitemsList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "index";
    }

    //添加页面显示
    @RequestMapping(value = "/add")
    public String add(){

        return "addPeitems"
                ;
    }

    //添加信息
    @RequestMapping(value = "/add.do")
    public String addPeitems(Peitems peitems)throws Exception{
        if (peitemsService.add(peitems)){
            return "redirect:/peitems/list";
        }
            return "addPeitems";
    }

    //根据id删除
    @RequestMapping(value = "/del")
    public String delPeitems(@RequestParam int itemid) throws Exception{
        if (peitemsService.del(itemid)){
            return "redirect:/peitems/list";

        }
            return "index";
    }

    //根据id查询信息
    @RequestMapping("/updateById")
    public String getById(@RequestParam int itemid, HttpServletRequest request)throws Exception{
        Peitems param=peitemsService.getitemid(itemid);
        Object[] pr={param.getItemid(),param.getItemname(),param.getTypeid(),param.getNecessary(),param.getRef(),param.getPrice(),param.getInfo()};
        request.setAttribute("pr",pr);
        logger.debug("项目名字：============"+param.getItemname());
        return "updPeitems";
    }
    //修改tools
    @RequestMapping(value = "/update.do")
    public String updatePeitems(Peitems param) throws Exception{
        if(peitemsService.upd(param)){
            return "redirect:/peitems/list";
        }
        return "updPeitems";
    }
    //分页
    @RequestMapping(value = "/pageList",method = RequestMethod.GET)
    public String pageLists(Integer pageIndex,HttpSession session)throws Exception{
        if (pageIndex==null){
            pageIndex=1;
        }
        PageUtil<Peitems> pageUtil=new PageUtil<>();
        pageUtil.setPageindex(pageIndex);
        pageUtil.setPagesize(2);
        peitemsService.getPageUtil(pageUtil);
        session.setAttribute("pageUtil",pageUtil);
        return "index";
    }
    //重命名验证
    @ResponseBody
    @RequestMapping("/exits.do")
    public Object exits(@RequestParam String itemname)throws Exception{
        Map<String,String>map=new HashMap<>();
        if (StringUtils.isNullOrEmpty(itemname)){
            map.put("devCode","exist");
        }else {
            Peitems user = peitemsService.exist(itemname);
            if (user != null){
                map.put("itemname","exist");
            }else {
                map.put("itemname","noExist");
            }
        }
        return JSONArray.toJSONString(map);
    }
    @RequestMapping(value = "/dologin",method = RequestMethod.POST)
    public String doLogin(String itemname, String ref, HttpServletRequest request, HttpSession session) throws Exception {
        logger.debug("==========================doLogin");
        Peitems user=peitemsService.login(itemname,ref);
        if (null!=user){
            session.setAttribute("peitemsService",user);
            return "redirect:/peitems/pageList";
        }else{
            request.setAttribute("error","用户密码不正确");
            return "login";
        }
    }
    }
